class DatasheetsController < ApplicationController
	before_filter :login_required, :except => [:index]
	
  def index
    @datasheets = Datasheet.find(:all)
  end

  def new
    @datasheet = Datasheet.new
  end

  def create
    @datasheet = Datasheet.new(params[:datasheet])
    @attachment = Attachment.create
    @attachable = params[:controller_type].singularize.capitalize.constantize.find(params[:id])
    @attachable.attachments << @attachment
    @datasheet.attachments << @attachment

    if @datasheet.save
      flash[:notice] = 'Datasheet was successfully uploaded.'
      if params[:controller_type] == 'categories'
      	redirect_to menu_name_path(@attachable.menu_name)
			else
				redirect_to path_for_vail(@attachable)
			end
    else
      render :action => "new"
    end
  end

  def destroy
    @datasheet = Datasheet.find(params[:id])
    @datasheet.destroy

    respond_to do |format|
      format.html { redirect_to datasheets_url }
      format.xml  { head :ok }
    end
  end
end
